// MyNumericBox.cpp: implementation of the CMyNumericBox class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "8085.h"
#include "MyNumericBox.h"
#include "Resource.h"
#include "GlobalFunctions.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

char *CMyNumericBox::m_szCaption;
char CMyNumericBox::m_szNumber[1024];
int CMyNumericBox::m_nButtonPressed=IDCANCEL;
int CMyNumericBox::m_nReads=0;

CMyNumericBox::CMyNumericBox()
{

}

CMyNumericBox::~CMyNumericBox()
{

}

BOOL CALLBACK Dlg_Proc (HWND hDlg, UINT uMsg,WPARAM wParam, LPARAM lParam) 
{
   BOOL fProcessed = TRUE;
   switch (uMsg) 
   {
       case WM_INITDIALOG:
           {
               RECT r;
               HWND desktopHandle;
               RECT desktopRect;
                
               desktopHandle = GetDesktopWindow();
               SetDlgItemText(hDlg,IDC_CAPTION,CMyNumericBox::GetCaption());
               SetDlgItemText(hDlg,IDC_GOTO_ADDRESS,(LPCTSTR)lParam);
               GetClientRect(hDlg,&r);
               GetClientRect(desktopHandle,&desktopRect);
               MoveWindow(hDlg,desktopRect.right/2,
                desktopRect.bottom/2,r.right,r.bottom+r.bottom/3,TRUE);
           }
           break;

       case WM_CLOSE:
           EndDialog(hDlg,0);
           return FALSE;

       case WM_COMMAND:
           if(wParam==IDOK)
           {
               //CMyNumericBox::int x;
               HWND h = GetDlgItem(hDlg,IDC_GOTO_ADDRESS);
               GetDlgItemText(hDlg,IDC_GOTO_ADDRESS,
                   CMyNumericBox::GetBuffer(),sizeof(CMyNumericBox::m_szNumber)-1);
               CMyNumericBox::SetButtonPressed(IDOK);
               EndDialog(hDlg,0);
           }
           else if(wParam==IDCANCEL)
           {
               CMyNumericBox::SetButtonPressed(IDCANCEL);
               EndDialog(hDlg,0);
           }
           break;

       case WM_KILLFOCUS:
            SendDlgItemMessage(hDlg,0,WM_SETFOCUS,wParam,lParam);
            break;

       case WM_SETFOCUS:    
           return FALSE;
       default:
          return FALSE;
   }
   return TRUE;
}

BOOL CMyNumericBox::ReadFromKeyboard(int &nResult, int nLow, int nHigh, char *szInitialStr)
{
    // since all member variables are static only one active call to this
    // function is allowed
    if(m_nReads>0)
    {
        ShowErrorMessage("Internal Error: 101");
        return FALSE;
    }
    ++m_nReads;
    while(1)
    {
        DialogBoxParam(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDD_GOTODIALOG),
            NULL,Dlg_Proc,LPARAM(szInitialStr));
        if(m_nButtonPressed==IDCANCEL)
        {
            --m_nReads;
            return FALSE;
        }
        static char str[256];
        int number;
        strncpy(str,m_szNumber,127);
        if(!::IsNumber(str,number))
        {
            ShowErrorMessage("Invalid Number");
            continue;
        }
        else
        {
            if(number>nHigh || number<nLow)  
            {
                sprintf(str,"Number should be within %d and %d",nLow,nHigh);
                ShowErrorMessage(str);
                continue;
            }
        }
        nResult = number;
        --m_nReads;
        return TRUE;
    }
}

char *CMyNumericBox::GetBuffer()
{
    return m_szNumber;
}

void CMyNumericBox::SetButtonPressed(int id)
{
    m_nButtonPressed = id;
}

void CMyNumericBox::SetCaption(char *str)
{
    m_szCaption = str;
}

char * CMyNumericBox::GetCaption()
{
    return m_szCaption;
}

void CMyNumericBox::DecrementRead()
{
    m_nReads--;
}
